﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using App.Core.Extensions;
using App.Data;

public partial class Pages_Admin_CustomerList : BasePage
{
    private int _ID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            CheckAndDeleteData();
            BindCustomerList(1);
        }
    }
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void CheckAndDeleteData()
    {
        if (String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.DELETE), "True", false) == 0)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            var customers = context.Customers.FirstOrDefault(P => P.ID == _ID);
            if (customers == null)
                WebUtil.ShowMessageBox(divMessage, "Sorry! Requested Customer was not found for delete.", true);
            else
            {
                customers.IsDeleted = true;
                customers.Modified = DateTime.Now;
                //customers.mo = SessionCache.CurrentUser.ID;
                context.SaveChanges();

                var contactsQuery = from P in context.Contacts where P.CustomerID == _ID && !P.IsDeleted 
                               select P;

                List<Contact> contacts = contactsQuery.ToList();
                for (int i = 0; i < contacts.Count; i++) {
                    Contact contact = contacts[i];
                    contact.IsDeleted = true;
                    contact.Modified = DateTime.Now;
                    contact.ModifiedBy = SessionCache.CurrentUser.CustomerID;
                    context.SaveChanges();
                }

                var usersQuery = from P in context.Users
                                    where P.CustomerID == _ID && !P.IsDeleted
                                    select P;

                List<User> users = usersQuery.ToList();
                for (int i = 0; i < users.Count; i++)
                {
                    User user = users[i];
                    user.IsDeleted = true;
                    user.Modified = DateTime.Now;                    
                    context.SaveChanges();
                }

                    WebUtil.ShowMessageBox(divMessage, "Customer Deleted Successfully.", true);
            }
        }
    }


    protected void BindCustomerList(int pageNumber)
    {
        int totalRecord = 0;
        UtilityDAO dao = new UtilityDAO();
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_CUSTOMERS, null, pageNumber, ConfigReader.CommonListPageSize, out totalRecord);
        ucUserList.DataSource = ds.Tables[0];
        //ucUserList.EditLink = "/Pages/Admin/NewsEdit.aspx?" + AppConstants.QueryString.ID + "={0}";
        ucUserList.DeleteLink = Request.Url.AbsolutePath + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.DELETE + "=True";
        ucUserList.DataBind();

        divListingContainer.Visible = true;

        ///Bind Pager
        ucUserListPager.TotalRecord = totalRecord;
        ucUserListPager.PageNo = pageNumber;
        ucUserListPager.PageSize = ConfigReader.CommonListPageSize;
        ucUserListPager.TotalRecordMessage = "Total {0} Customer(s) Found.";
        ucUserListPager.DataBind();

    }

    protected void ucUserListPager_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindCustomerList(e.PageIndex);
    }
}